
import mediaquery from '@system.mediaquery';
// abilityType: 0-Ability; 1-Internal Ability
const ABILITY_TYPE_EXTERNAL = 0;
const ABILITY_TYPE_INTERNAL = 1;
// syncOption(Optional, default sync): 0-Sync; 1-Async
const ACTION_SYNC = 0;
const ACTION_ASYNC = 1;
const ACTION_MUTE_SWITCH = 1000;
const ACTION_MUTE_STATE = 1001;
const ACTION_SET_VOLUME = 1002;
const ACTION_GET_VOLUME = 1003;

async function getMuteState() {
    console.debug("mute state");
    var action = {};
    action.bundleName = 'top.january.test';
    action.abilityName = 'top.january.test.MaininternalAbility';
    action.messageCode = ACTION_MUTE_STATE
    action.abilityType = ABILITY_TYPE_INTERNAL;
    action.syncOption = ACTION_SYNC;

    var result = await FeatureAbility.callAbility(action);
    var ret = JSON.parse(result);
    console.debug("mute state ret code: " + ret.code);
    return ret.code;
};

async function setVolume(stream, volume) {
    var actionData = {};
    actionData.stream = stream;
    actionData.volume = volume;
    var action = {};
    action.bundleName = 'top.january.test';
    action.abilityName = 'top.january.test.MaininternalAbility';
    action.messageCode = ACTION_SET_VOLUME;
    action.abilityType = ABILITY_TYPE_INTERNAL;
    action.syncOption = ACTION_SYNC;
    action.data = actionData;
    var result = await FeatureAbility.callAbility(action);
    var ret = JSON.parse(result);
    return ret.code;
}

async function getVolume(stream) {
    var actionData = {};
    actionData.stream = stream;
    var action = {};
    action.bundleName = 'top.january.test';
    action.abilityName = 'top.january.test.MaininternalAbility';
    action.messageCode = ACTION_GET_VOLUME;
    action.abilityType = ABILITY_TYPE_INTERNAL;
    action.syncOption = ACTION_SYNC;
    action.data = actionData;
    var result = await FeatureAbility.callAbility(action);
    var ret = JSON.parse(result);
    return ret.code;
}

export default {
    data: {
        title: "",
        muted: "false",
        music_volume : 0,
        ring_volume : 0,
        interval : 200,
        mv_adjust_ok : true,
        rv_adjust_ok : true
    },
    async onInit() {
        this.title = this.$t('strings.world');
        await this.updateVolumeState();
        console.debug("init mute status: " + this.muted);
    },
    async updateVolumeState() {
        this.music_volume = await getVolume("music");
        this.ring_volume = await getVolume("ring");
        this.muted = await getMuteState();
    },
    async muteSwitch(e) {
        console.debug("mute switch:");
        var action = {};
        action.bundleName = 'top.january.test';
        action.abilityName = 'top.january.test.MaininternalAbility';
        action.messageCode = ACTION_MUTE_SWITCH
        action.abilityType = ABILITY_TYPE_INTERNAL;
        action.syncOption = ACTION_SYNC;

        var result = await FeatureAbility.callAbility(action);
        var ret = JSON.parse(result);
        console.debug("muteSwitch ret code: " + ret.code);
        this.updateVolumeState();

    },
    async adjustMusicVolume(e) {
        if (this.mv_adjust_ok == true) {
            this.mv_adjust_ok = false;
            setTimeout(this.enableMVAdjust, this.interval);
            console.debug("change Music volume:" + e.value);
            var ret = await setVolume("music", e.value);
            console.debug("change Music volume to:" + ret);
            this.updateVolumeState();
        }
    },
    async adjustRingVolume(e) {
        if (this.rv_adjust_ok == true) {
            this.rv_adjust_ok = false;
            setTimeout(this.enableRVAdjust, this.interval);
            console.debug("change Ring volume:" + e.value);
            var ret = await setVolume("ring", e.value);
            console.debug("change Ring volume to:" + ret);
            this.updateVolumeState();
        }
    },
    enableMVAdjust() {
        this.mv_adjust_ok = true;
    },
    enableRVAdjust() {
        this.rv_adjust_ok = true;
    },

}

async function refresh() {

}
